#include "BoundingShapeRenderPass.h"
#include <SEEdit/Render/RenderFeature/DebugRenderFeature.h>
#include <SERender/Renderer/Renderer.h>
#include <SECore/Ecs/Scene.h>
#include <SEngine/Render/Descriptors/SceneDescriptor.h>
#include <SEngine/Ecs/Components/RigidBodyComp.h>
#include <SEngine/Ecs/Components/TransformComp.h>
#include <SEEdit/Descriptors/SelectActorDescriptor.h>

namespace SEngine
{

    BoundingShapeRenderPass::BoundingShapeRenderPass(Renderer &renderer)
        : RenderPass(renderer)
    {
    }

    void BoundingShapeRenderPass::DrawFrame()
    {
        auto & debugFeatrue = m_renderer.Descriptor<RenderFeature>::GetDescriptor<DebugRenderFeature>()->get();
        auto & selectActorDes = m_renderer.AnyDescriptor::GetDescriptor<SelectActorDescriptor>()->get();
        const glm::vec4 color = glm::vec4{0.0f, 1.0f, 0.0f, 1.0f};
        const float lineWidth = 1;
        if (selectActorDes.selectActor && selectActorDes.selectActor->get().HasComponent<RigidBodyComp>()) {
            auto & bodyComp = selectActorDes.selectActor->get().GetComponent<RigidBodyComp>()->get();
            auto & transComp = selectActorDes.selectActor->get().GetComponent<TransformComp>()->get();
            switch (bodyComp.shapeType)
            {
            case BaseShape::Box:
                debugFeatrue.DrawBox(bodyComp.offset, bodyComp.size, color, lineWidth, transComp.GetModel());
                break;                
            case BaseShape::Sphere:
                debugFeatrue.DrawCircle(transComp.GetModel() * glm::translate(glm::mat4{1.0f}, bodyComp.offset) * glm::mat4_cast(glm::quat(glm::radians(glm::vec3{0.0f, 0.0f, 0.0f}))) * glm::scale(glm::mat4{1.0f}, glm::vec3{bodyComp.radius}), color, lineWidth);                    
                debugFeatrue.DrawCircle(transComp.GetModel() * glm::translate(glm::mat4{1.0f}, bodyComp.offset) * glm::mat4_cast(glm::quat(glm::radians(glm::vec3{0.0f, 90.0f, 0.0f}))) * glm::scale(glm::mat4{1.0f}, glm::vec3{bodyComp.radius}), color, lineWidth);                    
                debugFeatrue.DrawCircle(transComp.GetModel() * glm::translate(glm::mat4{1.0f}, bodyComp.offset) * glm::mat4_cast(glm::quat(glm::radians(glm::vec3{90.0f, 0.0f, 0.0f}))) * glm::scale(glm::mat4{1.0f}, glm::vec3{bodyComp.radius}), color, lineWidth);                    
                break;                
            default:
                break;
            }
        }
    }

} // namespace SEngine
